import Domeform from './Domeform'
import List from './list';
import { useState } from 'react';
const TodoList = () => {
  const [objtext, setObjtext] = useState({
    objList: [{
      date: '2012-12-20',
      title: '标题',
      priority: '1'
    }]
  })
  const addKey = (val) => {
    let newList = objtext.objList
    if (newList.length === 0) {
      newList.push(val)
    } else {
      // 实现优先级
      for (let i = 0; i < newList.length; i++) {
        if (val.priority === newList[i].priority) {
          newList.splice(i, 0, val)
          break;
        } else {
          if (val.priority === '2') {
            let arr = []
            for (let i in newList) {
              if (newList[i].priority === '1') {
                arr.push(newList[i])
              }
            }
            newList.splice(arr.length, 0, val)
            break;
          } else if (val.priority === '3') {
            newList.splice(newList.length, 0, val)
            break;
          }
        }
      }
    }
    setObjtext(() => {
      return {
        ...objtext,
        objList: newList
      }
    })
  }
  return (<div className='TodoList'>
    {/* 主页面 */}
    <h2>todolist</h2>
    <Domeform addKey={addKey}></Domeform>
    <List list={objtext.objList}></List>
  </div>);
}

export default TodoList;